home *** CD-ROM | disk | FTP | other *** search
- Path: anvil.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Simple Program Question
- Date: 26 Feb 1996 15:26:52 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4gtfjsINNkec@anvil.ugrad.cs.ubc.ca>
- References: <4gsr9u$sk6@newsbf02.news.aol.com>
- NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
-
- In article <4gsr9u$sk6@newsbf02.news.aol.com>, Tycope <tycope@aol.com> wrote:
- >I am trying to write a non -interactive program that calculates all
- >integer triples (i, j, k) such that
- >0 < i < j < k < l and i + j + k = l. Print out the number of triples that
- >satisfy the requirements and print out every millionth triple. Can anyone
- >see where I am missing the boat in the following code. The program runs
- >and immediately terminates. Thanks in advance for any feedback.
-
- Why don't you just solve the linear diophantine equation directly?
-
- >#include <stdio.h>
- >
- >long int i, j, k, l;
- >long int count;
- >
- >int
- >main (void)
- >{
- > do
- > {
- > (l = i + j + k);
- > (i = 1);
- > (j = 2);
- > (k = 3);
- > (i++, j++, k++);
- > (++count);
- > if (count % 1000000 == 0)
- > {
- > printf("%ld(i) + %ld(j) + %ld(k) = %ld(l)", i, j, k, l);
- > }
- >
- > } while (0 < i < j < k < l);
- >
- > return (0);
- >}
-
- Where you are missing the boat is that you are incrementing the variables
- together. You need to learn about nested loops.
-
- Also, there is no such test in C as
-
- while (a < b < x)
-
- This is evaluated left to right (I think--the order is not relevant, because it
- is not useful):
-
- while ((a < b) < x)
-
- The (a < b) evaluates to a value that is 1 or zero based on whether the
- comparison is true. The 1 or 0 is then compared against x. This is far from
- what you want. The actual test is:
-
- .. while ((0 < i) && (i < j) && (j < k) && (k < l));
-
- This is not what you want either, because what it means is that as soon as you
- find a triplet that does not satisfy the condition 0 < i ... < l, your loop
- will bail.
-
-
- For arbitrary l, your problem has infinite solutions, are aware of that? There
- is no upper limit on an integer that is the sum of three other integers. Thus a
- program cannot find all solutions using finite-precision arithmetic in a finite
- amount of time.
-
- You need to set a goal for what values of 'l' you satisfy the other variables.
- There is no need to try values lower than 6, since the smallest possible
- triplet is obviously 1 + 2 + 3 = 6
-
- Try something along the lines of:
-
- #include <stdio.h>
-
- int main()
- {
- long i,j,k,l;
-
- for (l = 6; l < 100; l++) {
- printf("triplets for l = %d:\n",l);
- for (k = 3; k < l; k++)
- for (j = 2; j < k; j++) {
- i = l - j - k;
- if (i > 0 && i < j && i + j + k == l)
- printf("%d, %d, %d\n",i,j,k);
- }
- }
- return 0;
- }
-
- The i variable is determined by the other three since the equality must be
- satisified. Hence there is no innermost loop in the i variable. Of course, i
- must be tested to fit the constraints. The loop initial values and test
- conditions for j and k ensure that j < k < l.
-
-
- Sample output:
-
-
- triplets for l = 6:
- 1, 2, 3
- triplets for l = 7:
- 1, 2, 4
- triplets for l = 8:
- 1, 3, 4
- 1, 2, 5
- triplets for l = 9:
- 2, 3, 4
- 1, 3, 5
- 1, 2, 6
- triplets for l = 10:
- 2, 3, 5
- 1, 4, 5
- 1, 3, 6
- 1, 2, 7
- triplets for l = 11:
- 2, 4, 5
- 2, 3, 6
- 1, 4, 6
- 1, 3, 7
- 1, 2, 8
- triplets for l = 12:
- 3, 4, 5
- 2, 4, 6
- 1, 5, 6
- 2, 3, 7
- 1, 4, 7
- 1, 3, 8
- 1, 2, 9
- triplets for l = 13:
- 3, 4, 6
- 2, 5, 6
- 2, 4, 7
- 1, 5, 7
- 2, 3, 8
- 1, 4, 8
- 1, 3, 9
- 1, 2, 10
-
-
- I hope this is what you want. Good luck learning the fundamentals of computer
- programming.
- --
-
-